import Vue from 'vue';
import Vuex from 'vuex'
import $H from '../common/request.js';
import $C from '../common/config.js';
import io from '../common/uni-socket.io.js';
Vue.use(Vuex)


export default new Vuex.Store({
	state: {
		user: null,
		token: null,
		socket: null
	},
	actions: {
		//断开连接
		closeSocket({
			state,
			dispatch
		}) {
			if (state.socket) {
				state.socket.close()
			}
		},
		//连接socket
		connectSocket({
			state,
			dispatch
		}) {
			const Socket = io($C.socketUrl, {
				query: {},
				transports: ['websocket'],
				timeout: 5000
			})
			let onlineEvent = (e) => {
				uni.$emit('live', {
					type: 'online',
					data: e
				})
			}
			let commentEvent = (e) => {
				uni.$emit('live', {
					type: 'comment',
					data: e
				})
			}
			let giftEvent = (e) => {
				uni.$emit('live', {
					type: 'gift',
					data: e
				})
			}
			//连接成功
			Socket.on('connect', () => {
				console.log('已连接')
				state.socket = Socket
				const {
					id
				} = Socket
				//拿到链接唯一id，实现一对一通讯
				Socket.on(id, (e) => {
					let d = e.data
					if (e.meta.notoast) {
						return
					}
					if (d.action === 'error') {
						let msg = d.payload
						return uni.showToast({
							title: msg,
							icon: 'none'
						})
					}
				})
				//监听在线用户信息
				Socket.on('online', onlineEvent)
				//监听弹幕内容
				Socket.on('comment', commentEvent)
				//监听礼物接受
				Socket.on('gift', giftEvent)
			})
			//移除监听事件
			const removeListener = () => {
				if (Socket) {
					Socket.removeListener('online', onlineEvent)
					Socket.removeListener('comment', commentEvent)
					Socket.removeListener('gift', giftEvent)
				}
			}
			//连接失败
			Socket.on('error', () => {
				removeListener()
				state.socket = null
			})
			//断开
			Socket.on('disconnect', () => {
				removeListener()
				state.socket = null
			})
		},
		authMethod({
			state
		}, callback) {
			if (!state.token) {
				uni.showToast({
					title: '请先登录',
					icon:'none'
				})
				return uni.navigateTo({
					url: '/pages/login/login'
				})
			}
			callback()
		},
		initUser({
			state,
			dispatch
		}) {
			let user = uni.getStorageSync('user')
			let token = uni.getStorageSync('token')
			if (user) {
				state.user = JSON.parse(user)
				state.token = token
				//连接socket
				dispatch('connectSocket')
			}
		},
		logout({
			state,
			dispatch
		}) {
			$H.post('/logout', {}, {
				token: true,
				toast: false
			})
			dispatch('closeSocket')
			state.user = null;
			state.token = null
			uni.removeStorageSync('user')
			uni.removeStorageSync('token')
		},
		login({
			state,
			dispatch
		}, user) {
			state.user = user
			state.token = user.token
			uni.setStorageSync('user', JSON.stringify(user))
			uni.setStorageSync('token', user.token)
			dispatch('connectSocket')
		},
		getUserInfo({
			state
		}) {
			$H.get('/user/info', {
				token: true,
				noJump: true,
				toast: false
			}).then(res => {
				state.user = res
					state.user.avatar = $C.baseUrl + res.avatar
					uni.setStorage({
						key: 'user',
						data: JSON.stringify(state.user)
					})
			})
		}
	}
})
